package cn.basic.social.api.qwtalk.channel.internal.request;

import cn.basic.social.api.exception.ApiRuleException;
import cn.basic.social.api.CommonConstants;
import cn.basic.social.api.qwtalk.channel.internal.response.InternalOpenuseridToUseridResponse;
import cn.basic.social.api.qwtalk.BaseQwRequest;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName InternalOpenuseridToUseridRequest
 * @Description 将代开发应用或第三方应用获取的密文open_userid转换为明文userid
 * @Author zlf
 * @Date 2023/8/28 14:15
 */
public class InternalOpenuseridToUseridRequest extends BaseQwRequest<InternalOpenuseridToUseridResponse> {

    private final String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/batch/openuserid_to_userid";
    private String topHttpMethod = CommonConstants.HTTP_METHOD_POST;

    /**
     * open_userid列表，最多不超过1000个。必须是source_agentid对应的应用所获取
     */
    private List<String> openUseridList;

    /**
     * 企业授权的代开发自建应用或第三方应用的agentid
     */
    private Long sourceAgentid;

    /**
     * 构造函数传入相关参数
     * 
     * @param 
     * @author zlf
     * @date 14:22 2023/8/28
     */
    public InternalOpenuseridToUseridRequest(List<String> openUseridList, Long sourceAgentid) {
        this.openUseridList = openUseridList;
        this.sourceAgentid = sourceAgentid;
    }

    public List<String> getOpenUseridList() {
        return openUseridList;
    }

    public void setOpenUseridList(List<String> openUseridList) {
        this.openUseridList = openUseridList;
    }

    public Long getSourceAgentid() {
        return sourceAgentid;
    }

    public void setSourceAgentid(Long sourceAgentid) {
        this.sourceAgentid = sourceAgentid;
    }

    @Override
    public String getTopHttpMethod() {
        return this.topHttpMethod;
    }

    @Override
    public void setTopHttpMethod(String topHttpMethod) {
        this.topHttpMethod = topHttpMethod;
    }

    @Override
    public Map<String, String> getTextParams() {
        Map<String, String> txtParams = new HashMap<>();
        txtParams.put("source_agentid", this.sourceAgentid.toString());
        txtParams.put("open_userid_list", JSON.toJSONString(this.openUseridList));
        return txtParams;
    }

    @Override
    public Class<InternalOpenuseridToUseridResponse> getResponseClass() {
        return InternalOpenuseridToUseridResponse.class;
    }

    @Override
    public void check() throws ApiRuleException {
        if (this.sourceAgentid == null || this.sourceAgentid == 0) {
            throw new ApiRuleException("-1", "调用失败，缺少必要参数：sourceAgentid");
        }
        if (this.openUseridList == null || this.openUseridList.isEmpty()) {
            throw new ApiRuleException("-1", "调用失败，缺少必要参数：openUseridList");
        }
    }

    @Override
    public String getTopContentType() {
        return null;
    }

    @Override
    public void setTopContentType(String topContentType) {

    }

    @Override
    public String getRequestUrl() {
        return this.requestUrl;
    }
}
